﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.BUS;
using DoAn.DTO;
using System.Data;


namespace DoAn.Mua
{
    public partial class DanhMucSP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                LoadDanhMuc();
            }
        }
        public void LoadDanhMuc()
        {
            DataTable dt = new DataTable();
            dt = DanhMucBUS.loadDanhMuc();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count - 1; i++)
                {
                    DanhMucDTO dm = new DanhMucDTO();
                    dm.TenDanhMuc = dt.Rows[i]["TenDanhMuc"].ToString();
                    dm.MaDanhMucCha = int.Parse(dt.Rows[i]["MaDanhMucCha"].ToString());
                    dm.MaDanhMuc = int.Parse(dt.Rows[i]["MaDanhMuc"].ToString());
                    TreeNode pNode = new TreeNode(dm.TenDanhMuc, dm.MaDanhMuc.ToString());

                    if (dm.MaDanhMucCha == 0)
                    {
                        trvDanhMuc.Nodes.Add(pNode.Text);
                        for (int j = 0; j < dt.Rows.Count - 1; j++)
                        {
                            DanhMucDTO dm2 = new DanhMucDTO();
                            dm2.TenDanhMuc = dt.Rows[j]["TenDanhMuc"].ToString();
                            dm2.MaDanhMucCha = int.Parse(dt.Rows[j]["MaDanhMucCha"].ToString());
                            if (dm.MaDanhMuc == dm2.MaDanhMucCha)
                            {
                                TreeNode pNode2 = new TreeNode(dm2.TenDanhMuc, dm2.MaDanhMucCha.ToString());
                                trvDanhMuc.Nodes[i].Nodes.Add(pNode2.Text);
                            }
                        }
                    }
                }
            }

        }

        protected void trvDanhMuc_NodeClick(object source, DevExpress.Web.ASPxTreeView.TreeViewNodeEventArgs e)
        {
            SanDauGiaDTO san = new SanDauGiaDTO();
            DanhMucDTO dm = new DanhMucDTO();
            if (e.Node.Parent.Text != "Node")
            {
                dm.TenDanhMuc = e.Node.Text;
                dm.MaDanhMucCha = DanhMucBUS.layMaDanhMucCha(dm);
                Response.Write(dm.MaDanhMucCha);
                DataTable dt = new DataTable();
                dt = SanDauGiaBUS.laydssanphamtheomaDMcon(dm.MaDanhMucCha);
                //CollectionPager1.MaxPages = 10000;

                //CollectionPager1.PageSize = 5; // số items hiển thị trên một trang

                //CollectionPager1.DataSource = dt.DefaultView;

                //CollectionPager1.BindToControl = DataList1;

                //DataList1.DataSource = CollectionPager1.DataSourcePaged;
                DataList1.DataSource = dt;
                DataList1.DataBind();
            }
            else
            {
                dm.TenDanhMuc = e.Node.Text;
                dm.MaDanhMucCha = DanhMucBUS.layMaDanhMucCha(dm);
                List<int> t = DanhMucBUS.laylistma(dm.MaDanhMucCha);
                DataList1.DataSource = SanDauGiaBUS.laydssantheomaDM(t);
                DataList1.DataBind();
                //DataTable dt = new DataTable();

                //dt = SanDauGiaBUS.laydssantheomaDM(t);
                //CollectionPager1.MaxPages = 10000;

                //CollectionPager1.PageSize = 5; // số items hiển thị trên một trang

                //CollectionPager1.DataSource = dt.DefaultView;

                //CollectionPager1.BindToControl = DataList1;

                //DataList1.DataSource = CollectionPager1.DataSourcePaged;

                //DataList1.DataBind();

            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (txttimkiem.Text == "")
                Response.Write("<script>alert('Bạn chưa nhập đầy thông tin ')</script>");
            else
            {
                string ten = txttimkiem.Text;
                DataList1.DataSource = SanDauGiaBUS.laydstheoten(ten);
                DataList1.DataBind();
                //tim kiem
            }
        }
    }
}